#include<iostream>
#include<unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main()
{
    pid_t pid=fork();   //创建成功子进程pid会返回给父进程
                        //子进程皮pid返回0
    if(pid==0)
    {   
        cout<<"i am child precess"<<endl;
        int cnt=5;
        while(cnt--)
        {
            cout<<cnt<<" ";
            sleep(1);
            cout<<flush;
        }
        cout<<endl;
        return 0;
    }

    if(pid>0)
    {
        int status=0;

        int n=waitpid(pid,&status,0);
        if(n==-1)
        {
            perror("waitpid");
            return -1;
        }
        sleep(1);
        //打印状态码
        if((status&0x7F)==0)  //子进程正常退出
            cout<<"status is : "<<((status>>8) & 0xff)<<endl;
        else if(status&0x7f==0x7f)   //子进程被某个信号暂停
            cout<<"子进程停止"<<endl;
        else                    //子进程被信号终止
            {
                int cor=(status&0x80);
                int sig=(status&0x7f);
                cout<<"子进程被信号终止"<<"sig : "<<sig<<"   cor : "<<cor<<endl;
            }
    }
    else return -1;     //创建失败
    
    return 0;
}